﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BLL;
using System.Data;
namespace HotelManagement.Masterpage
{
    public partial class Default : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.Session["IDUser"] == null)
            {
                base.Session["Redirect"] = base.Request.RawUrl.ToString();
                base.Response.Redirect("~/ModSystem/Login.aspx");
            }
            try
            {
                this.ltrUsername.Text = base.Session["UserNme"].ToString();
                int IDUser = Convert.ToInt32(base.Session["IDUser"]);
                this.GetIDModule(base.Request.RawUrl.ToString().Split(new char[] { '?' })[0].ToString());
                this.LoadMenuMain(IDUser);
                if (!base.IsPostBack)
                {
                    this.UcMenuLeft1.LoadMenu(Convert.ToInt32(base.Session["IDModule"]));
                    this.LoadStatusOfRooms();
                }
                this.SetActiveMainMenu();
            }
            catch
            {
            }
        }
        private void GetIDModule(string linkfrom)
        {
            string link = linkfrom.Substring(linkfrom.IndexOf("Mod"));
            Sys_Module md = new Sys_Module();
            md.LinkDefault=link;
            DataTable dt = Sys_ModuleBLL.GetIdModule(md);
            if (dt.Rows.Count > 0)
            {
                object objID = dt.Rows[0]["IDModule"];
                if (objID != null)
                {
                    base.Session["IDModule"] = Convert.ToInt64(objID);
                }
            }
        }

        protected void lbtnThoat_Click(object sender, EventArgs e)
        {
            try
            {
                base.Session.Clear();
                base.Session.Abandon();
            }
            catch
            {
            }
            base.Response.Redirect("~/ModSystem/Login.aspx", true);
        }
        private void LoadStatusOfRooms()
        {
            try
            {
                DataTable tbl = Rec_RoomBLL.GetCount(0);
                if ((tbl != null) && (tbl.Rows.Count > 0))
                {
                    this.ltrRoomsQTY.Text = tbl.Rows[0]["TongPhong"].ToString();
                    this.ltrLodge.Text = tbl.Rows[0]["Lodge"].ToString();
                    this.ltrEmpty.Text = tbl.Rows[0]["Empty"].ToString();
                    this.ltrBooked.Text = tbl.Rows[0]["book"].ToString();
                    this.ltrDirty.Text = tbl.Rows[0]["dirty"].ToString();
                    this.ltrMainte.Text = tbl.Rows[0]["mainte"].ToString();
                }
            }
            catch
            {
            }
        }
        private void LoadMenuMain(int iduser)
        {
            try
            {
                DataTable tbl = Sys_PermistionBLL.GetModuleFolowUser(iduser);
                if (tbl != null)
                {
                    this.rptMenu.DataSource = tbl.DefaultView;
                    this.rptMenu.DataBind();
                    if ((tbl.Rows.Count > 0) && (base.Session["IDModule"] == null))
                    {
                        base.Session["IDModule"] = Convert.ToInt64(tbl.Rows[0]["IDModule"]);
                    }
                }
            }
            catch
            {
            }
        }
        private void SetActiveMainMenu()
        {
            try
            {
                int countitem = this.rptMenu.Items.Count;
                for (int i = 0; i < countitem; i++)
                {
                    LinkButton btn = (LinkButton)this.rptMenu.Items[i].FindControl("lbtnMainMenu");
                    if (!(btn.CommandArgument == base.Session["IDModule"].ToString()))
                    {
                        continue;
                    }
                    if (btn.CommandName != null)
                    {
                        if (!(btn.CommandName == "MQT"))
                        {
                            if (btn.CommandName == "MTM")
                            {
                                btn.CssClass = "clsActive";
                                return;
                            }
                            if (btn.CommandName == "MKS")
                            {
                                btn.CssClass = "clsActive";
                                return;
                            }
                            if (btn.CommandName == "MNH")
                            {
                                btn.CssClass = "clsActive";
                                return;
                            }
                            if (btn.CommandName == "MNS")
                            {
                                btn.CssClass = "clsActive";
                                return;
                            }
                            if (btn.CommandName == "MKT")
                            {
                                btn.CssClass = "clsActive";
                                return;
                            }
                        }
                        else
                        {
                            btn.CssClass = "clsActive";
                        }
                    }
                    return;     
                }
            }
            catch
            {
            }
        }
        protected void lbtnMainMenu_Click(object sender, EventArgs e)
        {
            string link = "";
            try
            {
                LinkButton btn = (LinkButton)sender;
                if (btn != null)
                {
                    base.Session["IDModule"] = Convert.ToInt64(btn.CommandArgument.ToString());
                    if (btn.CommandName != null)
                    {
                        if (!(btn.CommandName == "MQT"))
                        {
                            if (btn.CommandName == "MKS")
                            {
                                link = "~/ModKhachSan/QL_KhachSan.aspx";
                            }
                            if (btn.CommandName == "MKT")
                            {
                              link = "~/ModKeToan/doanhthukhachsan.aspx";
                            }
                        }
                        else
                        {
                            link = "~/ModSystem/NguoiDung.aspx";
                        }
                    }
                }
                 if (link != "")
                {
                    base.Response.Redirect(link, true);
                }
            
            }
            catch
            {
            }
           
        }
    }
}